package com.iplatform.file.function.meta.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.iplatform.file.function.meta.dao.NeuralNetworkMapper;
import com.iplatform.file.function.meta.domain.PreMetaDataBean;
import com.iplatform.file.function.meta.service.NetworkService;
import com.iplatform.file.function.meta.utils.JdbcUtil;
import com.iplatform.file.function.meta.vo.DbConfigVo;
import com.iplatform.file.function.meta.vo.PreMetaDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;


/**
 * Created/**
 * Created with IntelliJ IDEA.
 * User: lh
 * Date: 2021/01/06 15:46
 * Description:
 * Version: V1.0
 */
@Service
public class NetworkServiceImpl implements NetworkService {

    @Autowired
    private NeuralNetworkMapper neuralNetworkMapper;

    /**
     * 声明变量 static
     */
    public static Map<String, DataSource> dataSourceMap = new HashMap<>();

    @Override
    public List<JSONObject> getTables() {
        String tableName = new String();
        String comments = new String();
        Connection conn;
        List<JSONObject> tables = new ArrayList<>();
        String whiteList = "META";
        try {
            // 获取数据库配置
            DbConfigVo dbConfigVo = neuralNetworkMapper.queryById("3");
            DataSource dataSource = getDataSource("3");

            // 获取表
            conn = dataSource.getConnection();
            tables = JdbcUtil.getTablesFormDb(conn, dbConfigVo, tableName, comments, whiteList);
            for (int i = 0; i < tables.size(); i++) {
                String string = tables.get(i).toJSONString();
                int i1 = string.indexOf(",") - 1;
                int i2 = string.indexOf(":") + 7;
                String s = string.substring(i2, i1);
                if (!isNumeric(s)) {
                    tables.remove(i);
                    i--;
                } else if (s.length() != 17) {
                    tables.remove(i);
                    i--;
                }

            }
            return tables;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("数据库连接错误！" + e.toString());
        }
    }

    public static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    @Override
    public DataSource getDataSource(String dbConfigId) {
        DataSource dataSource = dataSourceMap.get(dbConfigId);
        // 获取数据库配置
        if (dataSource == null) {
            DbConfigVo dbConfigVo = neuralNetworkMapper.queryById(dbConfigId);
            dataSource = JdbcUtil.getDataSource(dbConfigVo);
            dataSourceMap.put(dbConfigId, dataSource);
        }
        return dataSource;
    }

    @Override
    public List<JSONObject> getTableColumns(String tableCode) {
        Connection conn = null;
        try {
            // 获取数据库配置
            DataSource dataSource = getDataSource("3");
            // 查询对应字段
            conn = dataSource.getConnection();
            List<JSONObject> columns = JdbcUtil.getTableColumns(conn, tableCode);
            return columns;
        } catch (SQLException e) {
            throw new RuntimeException("数据库连接错误！" + e.toString());
        }
    }

    @Override
    public PreMetaDataBean queryNetWorkById(Long id) {
        return neuralNetworkMapper.queryNetWorkById(id);
    }

    @Override
    public int insertPreservation(PreMetaDataBean preMetaDataBean) {
        preMetaDataBean.setCreateTime(new Date());
        return neuralNetworkMapper.insertPreservation(preMetaDataBean);
    }

    @Override
    public Integer querypreMetaDataBean(PreMetaDataBean preMetaDataBean) {
        return neuralNetworkMapper.querypreMetaDataBean(preMetaDataBean);
    }

    @Override
    public Integer queryUpdate(PreMetaDataBean preMetaDataBean) {
        return neuralNetworkMapper.queryUpdate(preMetaDataBean);
    }

    @Override
    public List<PreMetaDataBean> selectPreData(String tableCode, String outPutName, String inPutName,String putName) {
        String sql;
        if (putName == null || putName.trim().equals("")) {
            sql = "select " + outPutName + " as outPutName," + inPutName + " as inPutName from "
                    + tableCode;
        } else {
            sql = "select " + outPutName + " as outPutName," + inPutName + " as inPutName from "
                    + tableCode + " where " + inPutName + " like " + "'%" + putName + "%'";
        }
        return neuralNetworkMapper.selectPreData(sql);
    }

    @Override
    public List<String> sentPost(String sql) {
        return neuralNetworkMapper.sentPost(sql);
    }

    @Override
    public PreMetaDataVo selectDataById(Long id) {
        return neuralNetworkMapper.selectDataById(id);
    }

    @Override
    public int deletePreMetaData(Long id) {
        return neuralNetworkMapper.deletePreMetaData(id);
    }

    @Override
    public List<PreMetaDataVo> queryDataByConDition(PreMetaDataVo preMetaDataBean) {
        return neuralNetworkMapper.queryDataByConDition(preMetaDataBean);
    }

    @Override
    public PreMetaDataBean selectDatapreMeta(Long id) {
        return neuralNetworkMapper.selectDatapreMeta(id);
    }

    @Override
    public List<PreMetaDataVo> queryPreData() {

        return neuralNetworkMapper.queryPreData();
    }

    @Override
    public List<PreMetaDataBean> selectPreDataById(Long id) {
        PreMetaDataBean preMetaDataBean;
        preMetaDataBean = neuralNetworkMapper.selectDatapreMeta(id);
        String sql = "select " + preMetaDataBean.getId() + " as id," + preMetaDataBean.getInPutName() + " as inPutName," + preMetaDataBean.getOutPutName() + " as outPutName from " + preMetaDataBean.getTableCode();
        return neuralNetworkMapper.selectPreData(sql);
    }

    @Override
    public void updatePre(PreMetaDataBean preMetaDataBean){
        neuralNetworkMapper.updatePre(preMetaDataBean);
    }
}
